<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>

    <script src="../lib/vue2.js"></script>
  </head>
  <body>
    <div id="app">
      <!-- ref引用一个dom节点 -->
      <button @click="getRefs">info</button>
      <input type="text" name="username" id="001" ref="myInputOne" />
      <input type="text" name="username" id="002" ref="myInputTwo" />
      <!-- ref引用一个组件 -->
      <component-one ref="myComponentOne"></component-one>
    </div>

    <script>
      // 创建新组件
      Vue.component("componentOne", {
        template: `  <div>{{myInfo}}</div>`,
        data() {
          return {
            myInfo: "info from One",
          };
        },
      });

      new Vue({
        el: "#app",
        methods: {
          getRefs() {
            // 可以通过$refs和相应的key值获得节点
            console.log(this.$refs.myInputOne);
            // 通过引用获得组件的属性
            console.log(this.$refs.myComponentOne.myInfo);
            // 可以通过外界改变被引用组件的状态
            this.$refs.myComponentOne.myInfo = "change";
          },
        },
      });
    </script>
  </body>
</html>
